/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Riset1;

import amd.core.extraction.*;
import amd.core.CurrentDateAndTime;
import amd.core.data.DocumentPdf;
import amd.database.ConnectDB;
import amd.database.DbDocument;
import amd.database.DbTerm;
import amd.database.DbTermDocument;
import java.io.File;
import java.io.FileFilter;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
import Classes.Node;
import Classes.Doc;
//import Classes.TermDoc;
import Classes.PKembalian;
//import Classes.HasilExtract;

/**
 *
 * @author Urip, Setiadi, Irwan
 */
public class DocExtractor {
//    public static void insertToInvertedIndex(DocumentPdf d) {
//    }
//    public static HasilExtract extractAllDocument(File sourcePath, File targetPath, ConnectDB conn) {

    public static void extractAllDocument(File sourcePath, File targetPath, ConnectDB conn) {

//        HasilExtract inv = null;
//        Node terms[]= new Node[10000];
//        for(int l=0; l<10000; l++)
//            terms[l]=null;
//        TermDoc termDocs[][] = new TermDoc[10000][15];
        System.out.println(CurrentDateAndTime.getDateTime() + " Ekstraksi dokumen");
        System.out.println("Mau load file " + sourcePath);
        System.out.println(targetPath);
        long startTime = System.currentTimeMillis() / 1000;
//        DocumentPdf docPdf=null;

        File[] listOfFiles = sourcePath.listFiles(new FileFilter() {

            public boolean accept(File pathname) {
                return (pathname.getName().endsWith(".pdf"));
            }
        });

        int i = 0;
        int ke = -1;
        int nFiles = listOfFiles.length;
        DocumentPdf dPdf[] = new DocumentPdf[nFiles];
        System.out.println("nFiles = " + nFiles);
        if (nFiles > 0) {
            Doc docs[] = new Doc[nFiles];
            while (i < nFiles) {
                try {
                    PDDocument doc = PDDocument.load(listOfFiles[i]);
//                    dPdf[i] = extract(conn, listOfFiles[i], terms, targetPath);
                    dPdf[i] = extract(conn, listOfFiles[i], targetPath);

                    if (dPdf[i] != null) {
                        saveDocPdfToDB(conn, dPdf[i]);
                        doc.save(targetPath.getPath().concat("/" + listOfFiles[i].getName()));
                        Doc docc = new Doc();
                        docc.init();
                        docs[i] = docc;
                        docs[i].setId_Doc(dPdf[i].getIdDoc());
                        docs[i].setDpdf(dPdf[i]);
                    }
                    doc.close();
                    listOfFiles[i].delete();
                } catch (Exception ex) {
                    System.out.println("ada error saat mengekstak dokumen " + ex.toString());
                }
                i++;
            }
//            System.out.println("nFiles = "+nFiles);
//            int mx = max(dPdf,nFiles);
//            terms[mx]=null;
//            System.out.println("mx = "+mx);
//            int ii;
//            for(ii=0; ii<mx; ii++) {
//                for(i=0; i<nFiles; i++){
//                    try {
//                        TermDoc termDoc = new TermDoc();
//                        termDoc.init();
//                        termDoc.setKolom(i);
//                        if(ii+1 > dPdf[i].getTerm().size())
//                            termDoc.setFreq(0);
//                        else
//                            termDoc.setFreq(dPdf[i].getTerm().get(ii).getFrequency());
//                        termDocs[ii][i]=termDoc;
//                    } catch (Exception e) {
//                        System.out.println("Ada error new..."+e);
//                    }
//                }
//            }

//            int bb; int kk;
//            for(kk=0 ; kk < mx; kk++) {
//                System.out.println("");
//                System.out.print("Baris Print = "+kk+"\t");
//                System.out.print(terms[kk].getT()+" ");
//                bb = 0;
//                while (bb<nFiles) {
//                    try{
//                        System.out.print(" "+termDocs[kk][bb].getFreq());
//                        bb++;
//                        } catch (Exception e) {
//                            System.out.println("null pointer euy..."+e);
//                    }
//                }
//            }
//            inOrder(terms,0);
//            inv = new HasilExtract();
//            inv.setDocs(docs);
//            inv.setTermDocs(termDocs);
//            inv.setTerms(terms);
        } else {
            System.out.println("Tidak ada files pada source direktori");
            //inv = null;
        }

        long endTime = System.currentTimeMillis() / 1000;
        System.out.println("- Total waktu ekstraksi: "
                + ((endTime - startTime) / 3600) + " jam "
                + ((endTime - startTime) / 60) + " menit "
                + ((endTime - startTime) % 60) + " detik");
//        return jt.getDocPdf();
//        return inv;
    }
//    public static int max(DocumentPdf df[], int jfiles) {
//        int m=0;
//        System.out.println("Masuk max, jFiles = "+jfiles);
//        for(int i=0; i < jfiles; i++) {
//            try {
//            if(m < df[i].getTerm().size())
//                m = df[i].getTerm().size();
//            } catch (Exception e) {
//                System.out.println("Error null kalee..."+e);
//            }
//        }
//        return m;
//    }

//    public static DocumentPdf extract(ConnectDB conn, File f) {
//    public static DocumentPdf extract(ConnectDB conn, File f, Node terms[], File targetPath) {
    public static DocumentPdf extract(ConnectDB conn, File f, File targetPath) {
        DocumentPdf docPdf = null;
        try {
            PDDocument doc = PDDocument.load(f);
            PDFTextStripper stripper = new PDFTextStripper();
            String pdfContent = stripper.getText(doc);
            System.out.println("pdfContent=");
            System.out.println(pdfContent);
            String judulDocument = getJudul(pdfContent);
            String abstractDocument = getAbstract(pdfContent);
            boolean gagal = false;
            if (abstractDocument.trim().isEmpty()) {
                //printToLogFile("Dokumen \"" + f.getName()+"\" gagal diekstrak, tidak memiliki abstrak.");
                System.out.println("- Dokumen \"" + f.getName() + "\" gagal diekstrak, tidak memiliki abstrak.");
                gagal = true;
            }
            if (DbDocument.isPjgStringSamaExist(conn, pdfContent.length())) {
                if (DbDocument.isJudulExist(conn, judulDocument)) {
                    if (DbDocument.isExist(conn, abstractDocument)) {
                        System.out.println("File " + f.toString() + " ini sudah ada/pernah didownload");
                        gagal = true;
                    }
                }
            }
            if (!gagal) {
                docPdf = new DocumentPdf(f);
                docPdf.setTitle(judulDocument);
//                docPdf.setTitle(f.getName().substring(0, f.getName().length()-4));
//                docPdf.setLocation(f.getPath().replace('\\', '/'));
                String s = targetPath.getPath().concat("/" + f.getName());
                docPdf.setLocation(s.replace('\\', '/'));
                docPdf.setDocAbstract(abstractDocument);
                docPdf.setTerm(TermsExtractor.extract(pdfContent));
                docPdf.setPanjang(pdfContent.length());
                System.out.println("- Dokumen \"" + f.getName() + "\" berhasil diekstrak.");

                int i = 0;
                System.out.println("JUMLAH TERM = " + docPdf.getTerm().size());
//                while (i < docPdf.getTerm().size())
//                {
//                    Node n = new Node();
//                    n.init();
//                    n.setT(docPdf.getTerm().get(i).getContent());
//                    int j = available(terms);
//                    if (j==0)
//                        terms[0]=n;
//                    else
//                    {
//                        PKembalian pk = getPosisi(terms,n.getT());
//                        if(pk.getKika() != 1)
//                        {
//                            terms[j]=n;
//                            if(pk.getKika()<1)
//                                terms[pk.getPosisi()].setAki(j);
//                            else
//                                terms[pk.getPosisi()].setAka(j);
//                        }
//                    }
//                    i = i + 1;
//                }
                System.out.println("- Dokumen \"" + f.getName() + "\" berhasil diekstrak.");
            }
            doc.close();


//            docPdf = new DocumentPdf(f);
//            docPdf.setTitle(f.getName().substring(0, f.getName().length()-4));
//            System.out.println(docPdf.getTitle());
//            docPdf.setLocation(f.getPath().replace('\\', '/'));
//            System.out.println("Lokasi:"+docPdf.getLocation());
//            docPdf.setDocAbstract(abstractDocument);
//            System.out.println("Abstract-2:");
//            System.out.println(docPdf.getDocAbstract());
//            docPdf.setTerm(TermsExtractor.extract(pdfContent));
        } catch (Exception e) {
            System.out.println("error DocExtractor line 111 " + e.toString());
        }
        return docPdf;
    }

//    private static int available (Node n[]){
    public static int available(Node n[]) {
        int j = 0;
        while (j < n.length && n[j] != null) {
            j++;
        }
        return j;
    }

    private static void inOrder(Node n[], int d) {
        if (d == -1) {
            return;
        } else {
            System.out.println("Masuuk");
            inOrder(n, n[d].getAki());
            System.out.println(n[d].getT());
            inOrder(n, n[d].getAka());
        }
    }

    public static PKembalian getPosisi(Node n[], String s) {
        int kiri = 0;
        int kanan = 2;
        int tepat = 1;
        PKembalian pk = new PKembalian();
        int k = 0;
        int r = -1;
        boolean belum = true;
//        System.out.println("===> Masuk getPosisi-1");
        while (belum) {
//            System.out.println("s="+s);
//            System.out.println("n["+k+"]="+n[k].getT());
            if (s.compareTo(n[k].getT()) < 0) {
//                System.out.println("===> Masuk getPosisi-2");
                if (n[k].getAki() == -1) {
//                   System.out.println("===> Masuk getPosisi-3");
                    pk.setPosisi(k);
                    pk.setKika(kiri);
                    belum = false;
                } else {
                    k = n[k].getAki();
                }
            } else if (s.compareTo(n[k].getT()) > 0) {
//                System.out.println("===> Masuk getPosisi-4");
                if (n[k].getAka() == -1) {
                    pk.setPosisi(k);
                    pk.setKika(kanan);
                    belum = false;
                } else {
                    k = n[k].getAka();
                }
            } else {
                pk.setPosisi(k);
                pk.setKika(tepat);
                belum = false;
            }

        }
        return pk;

    }

    private static int generateIdDoc(int nDoc) {
        return nDoc + 1;
    }

    private static String getJudul(String stringInput) {
        String judulDocument = "";
        judulDocument = JudulExtractor.extract(stringInput);
        return judulDocument;
    }

    private static String getAbstract(String stringInput) {
        String abstractDocument = "";
        abstractDocument = AbstractExtractor.extract(stringInput);
        return abstractDocument;
    }

    private static int saveDocPdfToDB(ConnectDB conn, DocumentPdf docPdf) {
        System.out.println("\nMasuk saveDocPDFToDB <======");
//        String idDoc = "";
        int idDoc = 0;
        try {
            int nDoc = DbDocument.getNDocument(conn);
            idDoc = generateIdDoc(nDoc)/*countAll(db_document)*/;
            //DbDocument.insert(conn, idDoc);
            System.out.println("\n" + idDoc + " " + docPdf.getTitle() + " " + docPdf.getDocAbstract() + " " + docPdf.getLocation());
            DbDocument.insert(conn, String.valueOf(idDoc), docPdf.getTitle(), docPdf.getDocAbstract(), docPdf.getLocation(), docPdf.getPanjang());
            int i = 0;
            while (i < docPdf.getTerm().size()) {
                String termContent = docPdf.getTerm().get(i).getContent();
                int frequency = docPdf.getTerm().get(i).getFrequency();
//                if(termContent.contains("http") || termContent.contains("www") || termContent.contains("@"))
//                    System.out.println(termContent+"... diabaikan");
//                else {

                DbTermDocument.insert(conn, idDoc, termContent, frequency);
                if (!DbTerm.isExist(conn, termContent)) {
                    DbTerm.insert(conn, termContent);
                }

//                }
                i++;
            }
        } catch (Exception e) {
        }
        return idDoc;
    }
}
